/*
 * commons is a based project implemented
 * Copyright (C) 2024 Jasmine
 */
package com.commons.captcha.config;

import static com.google.code.kaptcha.Constants.*;

import java.util.Properties;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.PropertySource;

import com.commons.captcha.creator.KaptchaTextCreator;
import com.commons.captcha.props.CaptchaProperties;
import com.commons.core.utils.GeneralPropertySourceFactory;
import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.google.code.kaptcha.util.Config;

/**
 * @description
 * @version 1.0
 * @author Jasmine
 * @since 2024-01-08 15:54:54
 * @see CaptchaAutoConfig
 */
@AutoConfiguration
@EnableConfigurationProperties(CaptchaProperties.class)
@PropertySource(value = "classpath:default-captcha.yml", factory = GeneralPropertySourceFactory.class)
public class CaptchaAutoConfig {

    @Autowired
    private CaptchaProperties captchaProperties;

    @Bean(name = "captchaProducer")
    public DefaultKaptcha getKaptchaBean() {
        DefaultKaptcha defaultKaptcha = new DefaultKaptcha();
        Properties properties = new Properties();

        properties.setProperty(KAPTCHA_BORDER, captchaProperties.getBorder());
        properties.setProperty(KAPTCHA_BORDER_COLOR, captchaProperties.getBorderColor());
        properties.setProperty(KAPTCHA_BORDER_THICKNESS, captchaProperties.getBorderThickness());

        properties.setProperty(KAPTCHA_IMAGE_WIDTH, captchaProperties.getImageWidth());
        properties.setProperty(KAPTCHA_IMAGE_HEIGHT, captchaProperties.getImageHeight());

        properties.setProperty(KAPTCHA_TEXTPRODUCER_CHAR_STRING, captchaProperties.getCharString());
        properties.setProperty(KAPTCHA_TEXTPRODUCER_CHAR_LENGTH, captchaProperties.getCharLength());

        properties.setProperty(KAPTCHA_TEXTPRODUCER_FONT_NAMES, captchaProperties.getFontNames());
        properties.setProperty(KAPTCHA_TEXTPRODUCER_FONT_SIZE, captchaProperties.getFontSize());
        properties.setProperty(KAPTCHA_TEXTPRODUCER_FONT_COLOR, captchaProperties.getFontColor());
        properties.setProperty(KAPTCHA_TEXTPRODUCER_CHAR_SPACE, captchaProperties.getCharSpace());

        properties.setProperty(KAPTCHA_NOISE_IMPL, captchaProperties.getNoiseImpl());
        properties.setProperty(KAPTCHA_NOISE_COLOR, captchaProperties.getNoiseColor());
        properties.setProperty(KAPTCHA_OBSCURIFICATOR_IMPL, captchaProperties.getObscurificatorImpl());

        properties.setProperty(KAPTCHA_BACKGROUND_IMPL, captchaProperties.getBackgroundImpl());
        properties.setProperty(KAPTCHA_BACKGROUND_CLR_FROM, captchaProperties.getBackgroundClearFrom());
        properties.setProperty(KAPTCHA_BACKGROUND_CLR_TO, captchaProperties.getBackgroundClearTo());

        properties.setProperty(KAPTCHA_WORDRENDERER_IMPL, captchaProperties.getWordImpl());

        // properties.setProperty(KAPTCHA_SESSION_CONFIG_KEY, "kaptchaCode");

        Config config = new Config(properties);
        defaultKaptcha.setConfig(config);
        return defaultKaptcha;
    }

    @Bean(name = "captchaProducerMath")
    public DefaultKaptcha getKaptchaBeanMath() {
        DefaultKaptcha defaultKaptcha = new DefaultKaptcha();
        Properties properties = new Properties();

        properties.setProperty(KAPTCHA_BORDER, captchaProperties.getBorder());
        properties.setProperty(KAPTCHA_BORDER_COLOR, captchaProperties.getBorderColor());
        properties.setProperty(KAPTCHA_BORDER_THICKNESS, captchaProperties.getBorderThickness());

        properties.setProperty(KAPTCHA_IMAGE_WIDTH, captchaProperties.getImageWidth());
        properties.setProperty(KAPTCHA_IMAGE_HEIGHT, captchaProperties.getImageHeight());

        // properties.setProperty(KAPTCHA_TEXTPRODUCER_CHAR_STRING, captchaProperties.getCharString());
        // properties.setProperty(KAPTCHA_TEXTPRODUCER_CHAR_LENGTH, captchaProperties.getCharLength());

        properties.setProperty(KAPTCHA_TEXTPRODUCER_FONT_NAMES, captchaProperties.getFontNames());
        properties.setProperty(KAPTCHA_TEXTPRODUCER_FONT_SIZE, captchaProperties.getFontSize());
        properties.setProperty(KAPTCHA_TEXTPRODUCER_FONT_COLOR, captchaProperties.getFontColor());
        properties.setProperty(KAPTCHA_TEXTPRODUCER_CHAR_SPACE, captchaProperties.getCharSpace());

        properties.setProperty(KAPTCHA_NOISE_IMPL, captchaProperties.getNoiseImpl());
        properties.setProperty(KAPTCHA_NOISE_COLOR, captchaProperties.getNoiseColor());
        properties.setProperty(KAPTCHA_OBSCURIFICATOR_IMPL, captchaProperties.getObscurificatorImpl());

        properties.setProperty(KAPTCHA_BACKGROUND_IMPL, captchaProperties.getBackgroundImpl());
        properties.setProperty(KAPTCHA_BACKGROUND_CLR_FROM, captchaProperties.getBackgroundClearFrom());
        properties.setProperty(KAPTCHA_BACKGROUND_CLR_TO, captchaProperties.getBackgroundClearTo());

        // properties.setProperty(KAPTCHA_WORDRENDERER_IMPL, captchaProperties.getWordImpl());
        ///////////////

        // KAPTCHA_SESSION_KEY
        // properties.setProperty(KAPTCHA_SESSION_CONFIG_KEY, "kaptchaCodeMath");
        // 验证码文本生成器
        properties.setProperty(KAPTCHA_TEXTPRODUCER_IMPL, KaptchaTextCreator.class.getName());

        Config config = new Config(properties);
        defaultKaptcha.setConfig(config);
        return defaultKaptcha;
    }
}
